global with sharing class BatchDistributeOutlet implements Database.Batchable<sObject>,Database.Stateful{
    global BatchDistributeOutlet ()
    {}  
          
    global String stringSql{get;set;}
    global Id DistributeId{get;set;}
    global Boolean hasPhote{get;set;}
    global Id SurveyId{get;set;}
    
    global Database.QueryLocator start(Database.BatchableContext BC)    
    {        
        return Database.getQueryLocator(stringSql);    
    }    
    global void execute(Database.BatchableContext BC, List<sObject> scope)    
    {        
        List<TIMBASURVEYS__Recipient__c> listDt=new List<TIMBASURVEYS__Recipient__c>();        
        for(Sobject s:scope)        
        {            
            Account a=(Account)s;            
            TIMBASURVEYS__Recipient__c msg=new TIMBASURVEYS__Recipient__c();
            msg.Outlet__c=a.Id;
            msg.TIMBASURVEYS__isAnonymous__c=false;
            msg.TakePhoto__c=hasPhote;
            msg.Survey_Distribution__c=DistributeId;
            msg.TIMBASURVEYS__Survey__c=SurveyId;
            msg.TIMBASURVEYS__Summary_Created__c=false;
            msg.TIMBASURVEYS__IsPortalUser__c=false;//dd
            listDt.add(msg);        
        }      
        if(listDt.size()>0)  
            insert listDt; 
     }       
     global void finish(Database.BatchableContext BC){}
     
     static testmethod void test()
     {
          TIMBASURVEYS__Survey__c oSurvey=new  TIMBASURVEYS__Survey__c();           
          oSurvey.Name='问卷调查测试';           
          oSurvey.TIMBASURVEYS__AllowAnonymousRecipients__c=true;           
          oSurvey.TIMBASURVEYS__AllowMultiFill__c=true;           
          oSurvey.TIMBASURVEYS__CollectedResponses__c=0;           
          oSurvey.TIMBASURVEYS__FirstCollection__c=dateTime.now();           
          oSurvey.TIMBASURVEYS__QuestionsPerPage__c=1;           
          insert oSurvey;
          
         List<Account> accList=new List<Account>();
        for(Integer i=0;i<200;i++)
        {
             Account acc=new Account();            
             acc.Name='test'+String.valueOf(i);            
             acc.Channel__c='百货店';            
             acc.Contact__c='aaa';            
             acc.No_of_checkout__c=22;            
             acc.ContactPhoneNumber__c='1234567456';    
             acc.outletCode__c='test'+String.valueOf(i);
                     
             accList.add(acc);
         }
         
         insert accList;
         
         SurveyDistribution__c dis=new SurveyDistribution__c();
         dis.SurveyDistributionType__c='test';
         dis.Survey__c=oSurvey.Id;
         insert dis;
         
         Test.StartTest();
         BatchDistributeOutlet batch=new BatchDistributeOutlet();
          batch.stringSql='select Id from Account';                    
          batch.DistributeId=dis.Id;                    
          batch.hasPhote=false;                    
          batch.SurveyId=oSurvey.id;                     
          ID batchprocessid=Database.executeBatch(batch);
          Test.StopTest();
     }
}